package cvo.chess.client.entities.game;

import java.util.HashMap;

import cvo.chess.client.entities.chessboard.ChessboardCoordinate;
import cvo.chess.client.entities.chessboard.IChessboardCoordinate;

public class PositionManager {

	HashMap<IChessboardCoordinate, IPiece> positions = new HashMap<IChessboardCoordinate, IPiece>();

	public void putPiece(IPiece piece, IChessboardCoordinate position) throws Exception{
		if(positions.containsValue(piece)) {
			throw new Exception("Piece already on board, it can not have multiple positions at the same time");			
		}
		if(positions.containsKey(position)){
			throw new Exception("There is already a piece on this position, there can't be multiple pieces at the same position");			
		}
		
		positions.put(position, piece);		
	}
	
	public void movePieceTo(IPiece piece, ChessboardCoordinate bc) throws Exception{
		if(!positions.containsValue(piece)){
			throw new Exception("Piece not on board.");
		}				
		
		IChessboardCoordinate currentPos = positionOfPiece(piece);
		
		positions.remove(currentPos);
		positions.put(bc, piece);				
	}
	
	public IChessboardCoordinate positionOfPiece(IPiece piece) throws Exception{		
		for(IChessboardCoordinate key : positions.keySet()){
			if(positions.get(key).equals(piece)){
				return key;
			}
		}
		throw new Exception("piece not on board");
	}
	
	public IPiece pieceAtPosition(IChessboardCoordinate bc){
		return positions.get(bc);
	}
}
